package com.jjc.android.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.http.SslError
import android.os.Bundle
import android.view.KeyEvent
import android.webkit.SslErrorHandler
import android.webkit.WebView
import android.widget.FrameLayout
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.base.BaseActivity
import com.jjc.android.util.AndroidBug5497Workaround
import com.jjc.android.util.DataHolder
import com.jjc.android.view.CommonKt
import com.just.agentweb.AgentWeb
import com.just.agentweb.DefaultWebClient
import com.just.agentweb.WebChromeClient
import com.just.agentweb.WebViewClient
import com.lzy.okgo.OkGo


class BrowserActivity : BaseActivity(){

    companion object{

        fun start(context: Context,url:String? = null,title:String? = null,pageData:String? = null,javaScriptEnabled:Boolean = true){
            if(pageData==null&&url.isNullOrEmpty()) return
            val intent = Intent(context, BrowserActivity::class.java)
            if(!url.isNullOrEmpty()){
                intent.putExtra("url",url)
            }
            if(!title.isNullOrEmpty()){
                intent.putExtra("title",title)
            }
            if(!pageData.isNullOrEmpty()){
                val pageDataKey = "pageInfo"
                DataHolder.getInstance().setData(pageDataKey,pageData)
                intent.putExtra("infoKey",pageDataKey)
            }
            intent.putExtra("javaScriptEnabled",javaScriptEnabled)
            context.startActivity(intent)
        }
    }
    private var mUrl:String? = null
    private var mTitle:String? = null
    private var infoKey:String? = null
    override val layoutId: Int = R.layout.activity_browser
    private lateinit var mAgentWeb:AgentWeb

    override fun initView(savedInstanceState: Bundle?) {
        AndroidBug5497Workaround.assistActivity(this)
        val webLayout:FrameLayout = findViewById(R.id.webLayout)
        if(!mTitle.isNullOrEmpty()){
            setActivityTitle(mTitle!!)
        }
        val preAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(webLayout, FrameLayout.LayoutParams(-1, -1))
            .useDefaultIndicator(Color.parseColor("#62D99F"))
            .setWebChromeClient(mWebChromeClient)
            .setWebViewClient(mWebViewClient)
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
            .interceptUnkownUrl()
            .createAgentWeb()
            .ready()

        if(!infoKey.isNullOrEmpty()){
            mAgentWeb = preAgentWeb.get()
            mAgentWeb.webCreator.webView.loadDataWithBaseURL(null,CommonKt.getHtmlData((DataHolder.getInstance().getData(infoKey) as? String)?:""), "text/html",  "utf-8", null)
        }else{
            mAgentWeb = preAgentWeb.go(mUrl)
        }

        if(!javaScriptEnabled){
            mAgentWeb.agentWebSettings.webSettings.javaScriptEnabled = false
        }

    }

    private val mWebViewClient:WebViewClient = object : WebViewClient(){
        override fun onReceivedSslError(
            view: WebView?,
            handler: SslErrorHandler?,
            error: SslError?
        ) {
            handler?.proceed()
        }
    }



//    private val mWebViewClient:WebViewClient = object : WebViewClient(){
//        override fun onPageFinished(view: WebView?, url: String?) {
//            super.onPageFinished(view, url)
//            imgReset()
//        }
//
//        private fun imgReset() {
//            mAgentWeb.webCreator.webView.loadUrl(
//                "javascript:(function(){" +
//                        "var objs = document.getElementsByTagName('img'); " +
//                        "for(var i=0;i<objs.length;i++) " +
//                        "{"
//                        + "var img = objs[i]; " +
//                        " img.style.maxWidth = '100%'; img.style.height = 'auto'; " +
//                        "}" +
//                        "})()"
//            )
//        }
//    }

    private val mWebChromeClient: WebChromeClient = object : WebChromeClient() {
        override fun onReceivedTitle(view: WebView?, title: String?) {
            super.onReceivedTitle(view, title)
            if(mTitle.isNullOrEmpty()&&!title.isNullOrEmpty()){
                setActivityTitle(title)
            }
        }
    }
    private var javaScriptEnabled = true
    override fun setupArguments(args: Bundle?) {
        mUrl = args?.getString("url")
        mTitle = args?.getString("title")
        infoKey = args?.getString("infoKey")
        javaScriptEnabled = args?.getBoolean("javaScriptEnabled",true)?:true
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            true
        } else super.onKeyDown(keyCode, event)
    }

    override fun onPause() {
        mAgentWeb.webLifeCycle.onPause()
        super.onPause()
    }

    override fun onResume() {
        mAgentWeb.webLifeCycle.onResume()
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        mAgentWeb.webLifeCycle.onDestroy()
    }

}